package com.example.temp.mapper;

import com.example.temp.entity.ClassApplication;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface ClassApplicationMapper {
    
    // 添加班级申请
    @Insert("INSERT INTO class_application (class_id, student_id, reason) VALUES (#{classId}, #{studentId}, #{reason})")
    @Options(useGeneratedKeys = true, keyProperty = "applicationId")
    int insert(ClassApplication application);
    
    // 更新申请状态
    @Update("UPDATE class_application SET status = #{status}, handle_time = #{handleTime}, handle_teacher_id = #{handleTeacherId}, remark = #{remark} WHERE application_id = #{applicationId}")
    int updateStatus(ClassApplication application);
    
    // 根据ID查询申请
    @Select("SELECT a.*, c.class_name, s.name as student_name, t.name as teacher_name " +
            "FROM class_application a " +
            "LEFT JOIN class c ON a.class_id = c.class_id " +
            "LEFT JOIN student s ON a.student_id = s.studentID " +
            "LEFT JOIN teacher t ON a.handle_teacher_id = t.teacherID " +
            "WHERE a.application_id = #{applicationId}")
    ClassApplication findById(Integer applicationId);
    
    // 查询班级的申请列表
    @Select("<script>" +
            "SELECT a.*, c.class_name, s.name as student_name, t.name as teacher_name " +
            "FROM class_application a " +
            "LEFT JOIN class c ON a.class_id = c.class_id " +
            "LEFT JOIN student s ON a.student_id = s.studentID " +
            "LEFT JOIN teacher t ON a.handle_teacher_id = t.teacherID " +
            "WHERE a.class_id = #{classId} " +
            "<if test='status != null and status != \"\"'>" +
            "  AND a.status = #{status} " +
            "</if>" +
            "ORDER BY a.apply_time DESC" +
            "</script>")
    List<ClassApplication> findByClassId(Map<String, Object> params);
    
    // 查询学生的申请列表
    @Select("<script>" +
            "SELECT a.*, c.class_name, s.name as student_name, t.name as teacher_name " +
            "FROM class_application a " +
            "LEFT JOIN class c ON a.class_id = c.class_id " +
            "LEFT JOIN student s ON a.student_id = s.studentID " +
            "LEFT JOIN teacher t ON a.handle_teacher_id = t.teacherID " +
            "WHERE a.student_id = #{studentId} " +
            "<if test='status != null and status != \"\"'>" +
            "  AND a.status = #{status} " +
            "</if>" +
            "ORDER BY a.apply_time DESC" +
            "</script>")
    List<ClassApplication> findByStudentId(Map<String, Object> params);
    
    // 检查是否已经存在待处理的申请
    @Select("SELECT COUNT(*) FROM class_application WHERE class_id = #{classId} AND student_id = #{studentId} AND status = 'pending'")
    int countPendingApplication(@Param("classId") Integer classId, @Param("studentId") String studentId);
    
    // 检查学生是否已经在班级中
    @Select("SELECT COUNT(*) FROM student s WHERE s.class_id = #{classId} AND s.studentID = #{studentId}")
    int checkStudentInClass(@Param("classId") Integer classId, @Param("studentId") String studentId);
}
